package com.yyx.app1.incompatibility_function.UI;

import android.app.Activity;
import android.app.Fragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.yyx.app1.MyApp;
import com.yyx.app1.R;
import com.yyx.app1.incompatibility_function.Controls.Incompatibility_ListView_Control.Incompatibility_ListView_Adapter;
import com.yyx.app1.ui.BottomMenu_Fragment;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.FragmentById;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.ColorRes;
import org.androidannotations.annotations.res.StringRes;

import java.util.HashMap;

/**
 * Created by yuexing on 2015/9/8.
 */
@EActivity(R.layout.activity_incompatibility)
public class Incompatibility_Activity extends Activity {
    private Incompatibility_ListView_Adapter adapter;

    @ViewById
    TextView incompatibility_clear_button,search_title;

    @ViewById
    EditText incompatibility_edittext;

    @ViewById
    ListView incompatibility_listview;

    @ViewById
    Button search_searchbutton;

    @FragmentById
    Fragment searchdrup_bottommeun;

    @ViewById
    ImageView top_menu_ico;

    @StringRes
    String hotsearch;

    @ColorRes
    int app_color;

    @AfterViews
    public void init()
    {
        try
        {
            top_menu_ico.setImageBitmap(((MyApp)getApplication()).getBaseDataManager().getResDrawable(
                    R.drawable.whitethreeline, top_menu_ico.getWidth(),top_menu_ico.getHeight()));
            ((BottomMenu_Fragment)searchdrup_bottommeun).setShow("配伍禁忌");
            adapter = new Incompatibility_ListView_Adapter(this);
            changeAdapter();
            incompatibility_listview.setOverScrollMode(View.OVER_SCROLL_NEVER);
            incompatibility_listview.setDivider(new ColorDrawable(Color.rgb(203, 203, 203)));
            incompatibility_listview.setDividerHeight(1);
        }
        catch (Exception e)
        {
            Log.e("异常1510141452",e.toString());
        }
    }

    Runnable incompatibility_clear_button_change = new Runnable() {
        @Override
        public void run() {
            incompatibility_clear_button.setTextColor(Color.BLACK);
        }
    };

    @Click
    public void incompatibility_clear_button()
    {
        Toast.makeText(this,"请输入搜索内容",Toast.LENGTH_SHORT).show();
        incompatibility_clear_button.setTextColor(app_color);
        incompatibility_edittext.setText("");
//        incompatibility_edittext.setTextColor(Color.WHITE);
        search_title.setText(hotsearch);
        new Handler().postDelayed(incompatibility_clear_button_change, 300);
        changeAdapter();
    }

    @Click
    public void incompatibility_edittext()
    {
    }

    @Click
    public void search_searchbutton()
    {
        String str = incompatibility_edittext.getText().toString().trim();
        if (!str.isEmpty())
        {
            changeAdapter(str);
            search_title.setText("查询结果:" + adapter.getMap().size() + "种禁忌");
        }
        else
        {
            Toast.makeText(this,"请输入搜索内容",Toast.LENGTH_SHORT).show();
            changeAdapter();
            search_title.setText(hotsearch);
        }
    }

    @ItemClick(R.id.incompatibility_listview)
    public void listviewClicked(int position)
    {
        try
        {
            if (search_title.getText().toString().equals(hotsearch))
            {
                incompatibility_edittext.setText(adapter.getItem(position).toString());
            }
            else
            {
                String str1 = ((HashMap<String, String>) adapter.getItem(position)).get("firstMedicine");
                String str2 = ((HashMap<String, String>) adapter.getItem(position)).get("secondMedicine");
                Toast.makeText(this,str1+"   禁忌   "+str2,Toast.LENGTH_SHORT).show();
            }
        }
        catch (Exception e)
        {
            Log.e("异常1510141451",e.toString());
        }
    }

    public void changeAdapter()
    {
        adapter.findAll();
        incompatibility_listview.setAdapter(adapter);
    }

    public void changeAdapter(String str)
    {
        adapter.findAll(str);
        incompatibility_listview.setAdapter(adapter);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            finish();
        }
        return true;
    }
}
